import sys
import os

# IMPORT MODULES
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, Slot, Signal

# Main Window Class
class MainWindow(QObject):
    def __init__(self):
        QObject.__init__(self)

    # Static Info
    staticUser = "zorelu"
    staticPass = "123123zz"

    # Signals To Send Data
    signalUser = Signal(str)
    signalzorelu = Signal(str)
    signalPass = Signal(str)
    signalLogin = Signal(bool)
#Slot为返回到qml的函数
    # Function To Check Login
    @Slot(str, str)
    def checkLogin(self, a, b):
        if(self.staticUser.lower() == a.lower() and self.staticPass == b):
            # Send User And Pass to other qml
            print(a)

            #对于附加信号处理器，和前面讲到的普通信号处理器又有所不同。普通信号处理器，你先要知道信号名字，
            # 然后按照 on{Signal} 的语法来定义信号处理器的名字；
             #在py定义signalzorelu在qml触发为onSignalzorelu
            # 你只要通过附加类型名字引用它，把代码块赋值给它即可。下面是另外的代码片段：
            # 而附加信号处理器（自带），信号名字本身已经是 onXXX 的形式
           
            self.signalzorelu.emit("Username: " + a)
            self.signalPass.emit("Password: " + b)

            # Send Login Signal
            self.signalLogin.emit(True)
            
            print("Login passed!")
         
        else:
            self.signalLogin.emit(False)
            print("Login error!")

            


# INSTACE CLASS
if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    # Get Context
    main = MainWindow()
    engine.rootContext().setContextProperty("backend", main)

    # Load QML File
    engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))

    # Check Exit App
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())
